package com.be.beadmin.design.controller;

import com.alibaba.excel.EasyExcel;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.design.dto.input.ProcessDto;
import com.be.beadmin.design.dto.output.BopEx;
import com.be.beadmin.design.dto.output.ProcessEx;
import com.be.beadmin.design.service.ProcessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
* @author Jiahao Liu
* @date 2023-02-19
**/
@Api(tags = {"设计管理-工序定义"})
@RestController
public class ProcessController {

    @Autowired
    private  ProcessService processService;

    @ApiOperation("获取工序分页信息")
    @PostMapping(value = "/design/getProcessByPage")
    public ResultModel getProcessByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(processService.getProcessByPage(pageEntity)) ;
    }

    @ApiOperation("获取所有工序")
    @PostMapping("/design/getAllProcess")
    public ResultModel getAllProcess(){
        List<ProcessDto> processDtoList = processService.getAllProcess();
        return ResultModel.success(processDtoList);
    }

    @ApiOperation("添加工序")
    @PostMapping("/design/addProcess")
    public ResultModel addProcess(@RequestBody ProcessDto processDto){

        try {
            processService.addProcess(processDto);
            return ResultModel.success(ResultModel.ADD);
        } catch (BeadminException e) {
            return ResultModel.error(-1,e.getMessage());
        } catch (Exception e) {
            return ResultModel.error(-2,e.getMessage());
        }
    }

    @ApiOperation("删除工序")
    @PostMapping("/design/delProcess")
    public ResultModel delProcess(@RequestBody ProcessDto processDto){
        processService.delProcess(processDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }

    @ApiOperation("更新工序")
    @PostMapping("/design/updateProcess")
    public ResultModel updateProcess(@RequestBody ProcessDto processDto){
        processService.updateProcess(processDto);
        return ResultModel.success(ResultModel.UPDATE);
    }

    @ApiOperation("导出")
    @PostMapping("/design/exportProcess")
    public void exportProcess(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<ProcessEx> processPage = processService.getProcessByPage(pageEntity);
        List<ProcessEx> processExes = processPage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("Process接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), ProcessEx.class).sheet("sheet1").doWrite(processExes);
    }
}